
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: IdField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import org.dellroad.stuff.string.ParseContext;

/**
 * The {@code "sidekarId"} implicit field.
 *
 * <p>
 * This is the implicit field with name {@link Sidekar#ID_FIELD_NAME} containing an item's {@link Item#getSidekarId Sidekar ID}.
 * </p>
 *
 * <p>
 * Unlike normal fields, this field may be accessed even if the associated {@link Item} is detached.
 * </p>
 *
 * @see Item#getSidekarId
 * @see Database#getIdField
 */
public final class IdField extends ImplicitField<Long> {

    IdField() {
        super(Sidekar.ID_FIELD_NAME, Long.class);
    }

    @Override
    public Long get(Item<?> item) {
        return item.getSidekarId();
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseIdField(this);
    }

    @Override
    public String toString(Long value) {
        return value.toString();
    }

    @Override
    public Long fromString(ParseContext ctx) {
        return Long.parseLong(ctx.matchPrefix("-?[0-9]+").group());
    }
}

